import { tokenVarName, tokenPosition, replayUrl } from "@/config"
import { axios } from "./index"

/**
 * @param {object} options
 * @returns {Promise<any>}
 */
export default async function (options) {
	try {
		const { access_token, refresh_token } = await axios.post(
			replayUrl,
			{},
			{
				header: {
					Authorization: uni.getStorageSync("refresh_token") || "",
				},
			}
		)
		uni.setStorageSync("token", access_token)
		uni.setStorageSync("refresh_token", refresh_token)
		if (options.token) {
			if (tokenPosition == "data") {
				options.data[tokenVarName] = access_token
			} else if (tokenPosition == "header") {
				options.header[tokenVarName] = access_token
			}
		}
		return axios[options.method.toLocaleLowerCase()](options.url, options.data, options)
	} catch (error) {
		return Promise.reject(error)
	}
}
